package cn.tedu.charging.order.config;

import cn.tedu.charging.common.constant.AmqpComponentConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AmqpDeclareConfig {
    // 1.声明业务交换机 fanout
    @Bean
    public Exchange delayBillEx(){
        return ExchangeBuilder.fanoutExchange(AmqpComponentConst.DELAY_BILL_EX).build();
    }
    // 2.声明业务队列 包含3个队列属性ttl dl_ex dl_rk
    @Bean
    public Queue delayBillQueue(){
        return QueueBuilder.nonDurable(AmqpComponentConst.DELAY_BILL_QUEUE)
                // ttl 1000*60
                .withArgument("x-message-ttl",1000*60)
                // 绑定死信交换机
                .withArgument("x-dead-letter-exchange",AmqpComponentConst.DELAY_BILL_DL_EX)
                // 绑定死信路由
                .withArgument("x-dead-letter-routing-key",AmqpComponentConst.DELAY_BILL_DL_RK)
                .build();
    }
    // 3.把业务队列绑定给业务交换机
    @Bean
    public Binding delayBillBind(){
        return BindingBuilder.bind(delayBillQueue()).to(delayBillEx()).with("").noargs();
    }
    // 4.声明死信交换机
    @Bean
    public Exchange delayBillDlEx(){
        return ExchangeBuilder.directExchange(AmqpComponentConst.DELAY_BILL_DL_EX).build();
    }
    // 5.声明死信队列
    @Bean
    public Queue delayBillDlQueue(){
        return QueueBuilder.nonDurable(AmqpComponentConst.DELAY_BILL_DL_QUEUE).build();
    }
    // 6.死信组件的绑定 一定要使用死信路由key
    @Bean
    public Binding delayBillDlBinding(){
        return BindingBuilder.bind(delayBillDlQueue()).to(delayBillDlEx()).with(AmqpComponentConst.DELAY_BILL_DL_RK).noargs();
    }
}
